//
//  WeiboHeader.h
//  YuiKe
//
//  Created by yuike5 yuike5 on 11-10-24.
//  Copyright 2011 yuike. All rights reserved.
//

#ifndef YuiKe_WeiboHeader_h
#define YuiKe_WeiboHeader_h

#import "MBProgressHUD.h"
#import "WeiboCommentNode.h"
#import "WeiboNode.h"
#import "WeiboUserNode.h"
#import "WeiboEmotion.h"


@class WeiboBase;
@class ProductXmlModel;


#define SNS_ID_SINA_WEIBO 1
//#define SNS_ID_TENC_WEIBO 2
#define SNS_ID_TAOBAO_OPEN 3
#define SNS_ID_QQ_SHARE    4



@protocol WeiboAuthDelegate<NSObject>
-(UIWebView*) weiboAuthview;
-(void) authCallback; // 成功了才调用..
-(void) authCallbackCurrentRequest:(NSURLRequest*)request;
@end

@protocol WeiboCommitDelegate<NSObject>
-(void) commitCallback:(WeiboBase*)weibo;
-(void) commitCallbackError:(WeiboBase*)weibo reason:(NSString*)reason;
@end

@protocol WeiboGetFriendshipDelegate<NSObject>
-(void) getFriendshipCallback:(WeiboBase*)weibo following:(BOOL)following followedBy:(BOOL)followedBy;
-(void) getFriendshipError:(WeiboBase*)weibo;
@end

@protocol WeiboFollowDelegate<NSObject>
-(void) followCallback:(WeiboBase*)weibo idx:(long long)idx result:(BOOL)result;
@end

@protocol WeiboGetUserInfoDelegate<NSObject>
-(void) getUserInfoError: (WeiboBase*)weibo;
-(void) getUserInfoCallback:(WeiboBase*)weibo nick:(NSString*)name domain:(NSString*)url user:(WeiboUserNode*)user;
@end

//Short url/share/statuses
//http://open.weibo.com/wiki/Short_url/share/statuses




@protocol WeiboShortUrlClicksDelegate <NSObject>
-(void) getWeiboShortUrlClicksError:(WeiboBase*)weibo;
-(void) getWeiboShortUrlClicksCallback:(WeiboBase*)weibo url_short:(NSString*)url_short url_long:(NSString*)url_long clicks:(int)clicks;
@end

@protocol WeiboShortUrlShortenDelegate<NSObject>
-(void) getWeiboShortUrlShortenDelegateError:(WeiboBase*)weibo;
-(void) getWeiboShortUrlShortenDelegateCallback:(WeiboBase*)weibo url_short:(NSString*)url_short url_long:(NSString *)url_long  result:(bool)result;
@end

@protocol WeiboShortUrlShareCountsDelegate <NSObject>
-(void) getWeiboShortUrlShareCountsDelegateError:(WeiboBase*)weibo;
-(void) getWeiboShortUrlShareCountsDelegateCallback:(WeiboBase*)weibo url_short:(NSString*)url_short url_long:(NSString*)url_long share_counts:(int)share_counts;
@end

@protocol WeiboShortUrlShareStatusDelegate<NSObject>
-(void) getWeiboShortUrlShareStatusDelegateError:(WeiboBase*)weibo;
-(void) getWeiboShortUrlShareStatusDelegateCallback:(WeiboBase*)weibo url_short:(NSString*)url_short url_long:(NSString*)url_long share_statuses:(NSArray*)share_statuses;
@end

@protocol WeiboShortUrlCommentCountsDelegate <NSObject>
-(void) getWeiboShortUrlCommentCountsDelegateError:(WeiboBase*)weibo;
-(void) getWeiboShortUrlCommentCountsDelegateCallback:(WeiboBase*)weibo url_short:(NSString*)url_short url_long:(NSString*)url_long comment_counts:(int)comment_counts;
@end

@protocol WeiboShortUrlCommentCommentsDelegate <NSObject>
-(void) getWeiboShortUrlCommentCommentsDelegateError:(WeiboBase*)weibo;
-(void) getWeiboShortUrlCommentCommentsDelegateCallback:(WeiboBase*)weibo url_short:(NSString*)url_short url_long:(NSString*)url_long share_comments:(NSArray*)share_comments;
@end




@protocol WeiboSearchDelegate<NSObject>
-(void) searchError: (WeiboBase*)weibo;
-(void) searchCallback:(WeiboBase*)weibo totalnum:(long)totalnum alist:(NSArray*)alist;
@end

// 根据微博ID返回某条微博的评论列表 
@protocol WeiboCommentsShowDelegate <NSObject> 
-(void) getCommentsError:(WeiboBase*)weibo;
-(void) getCommentsCallback:(WeiboBase*)weibo totalnum:(long)totalnum alist:(NSArray*)alist;
@end

// 对一条微博进行评论 
@protocol WeiboCommentCreateDelegate <NSObject>
-(void) createCommentError:(WeiboBase*)weibo reason:(NSString*)reason;
-(void) createCommentCallback:(WeiboBase*)weibo idx:(long long)idx result:(WeiboCommentNode*)result;
@end

//根据微博ID获取单条微博内容 
@protocol WeiboNodeShowDelegate <NSObject>
-(void) getWeiboError:(WeiboBase*)weibo;
-(void) getWeiboCallback:(WeiboBase*)weibo result:(WeiboNode*)result;
@end

@protocol WeiboFriendsDelegate <NSObject>
-(void) getWeiboFriendsError:(WeiboBase*)weibo;
-(void) getWeiboFriendsCallback:(WeiboBase*)weibo alist:(NSArray*)alist hasNext:(BOOL)hasNext nextCursor:(int)cursor;
@end



// 子类的 UIWebViewDelegate 必须全部 super... call.....
@interface  WeiboBase  : NSObject<UIWebViewDelegate, MBProgressHUDDelegate> {
    NSString* _userName;
    NSString* _domainUrl;
    WeiboUserNode* _userNode;
    // 每个idx区分开，避免不同接口调用间的 交错 问题
    long long _backupGetFriendshipIdx; // 保存，回调的时候回调。
    long long _backupFollowIdx;
    long long _backupGetCommentsIdx;
    long long _backupCommentIdx;
    long      _backupGetFriendsNextCursor;
    long      _backupgetFriendsReqnum;
    NSString* _backupCommentContent;
    
    id<UIWebViewDelegate> authDelegateBackup;   
    id<WeiboAuthDelegate> _authDelegate;
    id _theDelegate;
}

@property (nonatomic, assign) long long backupGetFriendshipIdx;
@property (nonatomic, assign) long long backupFollowIdx;
@property (nonatomic, assign) long long backupGetCommentsIdx;
@property (nonatomic, assign) long long backupCommentIdx;
@property (nonatomic, assign) long backupGetFriendsNextCursor;
@property (nonatomic, assign) long backupgetFriendsReqnum;
@property (nonatomic, copy) NSString* backupCommentContent;
@property (nonatomic, copy) NSString* myUserName;
@property (nonatomic, copy) NSString* myDomainUrl;
//@property (nonatomic, retain) WeiboUserNode* myUserNode; // 后来的版本才加的，没调试过，貌似保存还存在问题。-- 放弃这个思路，因为需要把以前的用户兼容。

@property (nonatomic, assign) id<WeiboAuthDelegate> authDelegate;
@property (nonatomic, assign) id<UIWebViewDelegate> authDelegateBackup;

-(void) setOnetimeDelegate:(id) delegate;
//-(id<WeiboAuthDelegate>) getWeiboAuthDelegate;
-(id<WeiboCommitDelegate>) getWeiboCommitDelegate;
-(id<WeiboGetFriendshipDelegate>) getWeiboGetFriendshipDelegate;
-(id<WeiboFollowDelegate>) getWeiboFollowDelegate;
-(id<WeiboGetUserInfoDelegate>) getWeiboGetUserInfoDelegate;
-(id<WeiboShortUrlClicksDelegate>) getWeiboShortUrlClicksDelegate;
-(id<WeiboShortUrlShortenDelegate>) getWeiboShortUrlShortenDelegate;
-(id<WeiboShortUrlShareCountsDelegate>) getWeiboShortUrlShareCountsDelegate;
-(id<WeiboShortUrlShareStatusDelegate>) getWeiboShortUrlShareStatusDelegate;
-(id<WeiboShortUrlCommentCountsDelegate>) getWeiboShortUrlCommentCountsDelegate;
-(id<WeiboShortUrlCommentCommentsDelegate>) getWeiboShortUrlCommentCommentsDelegate;
-(id<WeiboSearchDelegate>) getWeiboSearchDelegate;
-(id<WeiboCommentsShowDelegate>) getWeiboCommentsShowDelegate;
-(id<WeiboCommentCreateDelegate>) getWeiboCommentCreateDelegate;
-(id<WeiboNodeShowDelegate>) getWeiboNodeShowDelegate;
-(id<WeiboFriendsDelegate>) getWeiboFriendsDelegate;


+(BOOL) isAuthorized;




-(void) setDelegateNilAll;
-(BOOL) isinWorking;
-(void) cancelAll;


// 这些接口都只能调用一次，上次完全完成后，才可以进行下一次，否则会存在 问题。
-(void) auth:(id<WeiboAuthDelegate>)delegate;
-(void) commit:(id<WeiboCommitDelegate>)delegate content:(NSString *)content image:(UIImage*)image;
-(void) signOutx;
-(void) _protected_signOut;
-(void) getFriendship:(id<WeiboGetFriendshipDelegate>)delegate idx:(long long)idx;
-(void) follow:(id<WeiboFollowDelegate>)delegate idx:(long long)idx;
-(void) getUserInfo:(id<WeiboGetUserInfoDelegate>)delegate;
-(void) search:(id<WeiboSearchDelegate>)delegate_ qvalue:(NSString*)qvalue;

-(int) getFriendsFirstIntvalue;
-(int) getFriendsMaxReqnum;
-(void) getFriends:(id<WeiboFriendsDelegate>)delegate_ page:(int)page reqnum:(int)reqnum;



/**
format 	返回数据的格式（json或xml）
flag 	标识。0－转播列表 1－点评列表 2－点评与转播列表
rootid 	转发或回复的微博根结点id（源微博id）
pageflag 	分页标识（0：第一页，1：向下翻页，2：向上翻页）
pagetime 	本页起始时间（第一页：填0，向上翻页：填上一次请求返回的第一条记录时间，向下翻页：填上一次请求返回的最后一条记录时间）
reqnum 	每次请求记录的条数（1-100条）
twitterid 	翻页用，第1-100条填0，继续向下翻页，填上一次请求返回的最后一条记录id */
#define PAGE_FLAG_FIRST 0
#define PAGE_FLAG_NEXT  1
#define PAGE_FLAG_PREx  2
-(void) getCommentsTENC:(id<WeiboCommentsShowDelegate>)delegate_ 
                 rootid:(long long)rootid
               pageflag:(int)pageflag
               pagetime:(long long)pagetime
                 reqnum:(int)reqnum
              twitterid:(long long)twitterid;
/**
 必选 	类型及范围 	说明
 source 	true 	string 	申请应用时分配的AppKey，调用接口时候代表应用的唯一身份。（采用OAuth授权方式不需要此参数）
 id 	true 	int64 	指定要获取的评论列表所属的微博消息ID
 count 	false 	int，默认值20，最大值200。 	单页返回的记录条数。
 page 	false 	int，默认值1。 	返回结果的页码。 
 */
-(void) getCommentsSINAv1:(id<WeiboCommentsShowDelegate>)delegate_ 
                      idx:(long long)idx
                    count:(int)count
                     page:(int)page;
/**
 必选 	类型及范围 	说明
 source 	false 	string 	采用OAuth授权方式不需要此参数，其他授权方式为必填参数，数值为应用的AppKey。
 access_token 	false 	string 	采用OAuth授权方式为必填参数，其他授权方式不需要此参数，OAuth授权后获得。
 id 	true 	int64 	需要查询的微博ID。
 since_id 	false 	int64 	若指定此参数，则返回ID比since_id大的评论（即比since_id时间晚的评论），默认为0。
 max_id 	false 	int64 	若指定此参数，则返回ID小于或等于max_id的评论，默认为0。
 count 	false 	int 	单页返回的记录条数，默认为50。
 page 	false 	int 	返回结果的页码，默认为1。
 filter_by_author 	false 	int 	作者筛选类型，0：全部、1：我关注的人、2：陌生人，默认为0。 
 */
-(void) getCommentsSINAv2:(id<WeiboCommentsShowDelegate>)delegate_ 
                      idx:(long long)idx 
                 since_id:(long long)since_id
                   max_id:(long long)max_id
                    count:(int)count 
                     page:(int)page;

-(void) addShare:(id<WeiboCommitDelegate>)delegate_ content:(NSString*)content 
           title:(NSString*)title url:(NSString*)url summary:(NSString*)summary
        imageurl:(NSString*)imageurl;

-(void) comment:(id<WeiboCommentCreateDelegate>)delegate_ idx:(long long)idx  comment:(NSString*)comment;
-(void) getNode:(id<WeiboNodeShowDelegate>)delegate_ idx:(long long)idx;




-(NSArray*)emotions;
-(NSArray*)urlStringsforCookies;

-(NSString*) title;
-(NSString*) cpsUrlTrans:(NSString*)cpsUrl;
-(UIImage*) theIcon32x32;
-(UIImage*) theIcon48x48;
-(int) theSnsId;
-(NSString*) nick;
-(BOOL) isAuthorized;
//-(BOOL) isOpened; // 界面逻辑。。是否启用
//-(void) setOpened:(BOOL)opened;

-(BOOL)havUserNameDomain;
-(void) reloadAuthInfo;

-(NSString*) rootNick;
+(NSString*) rootNick;
-(NSString*) _protected_nick;


-(void) loadCurrentUserInfo;
-(void) saveCurrentUserInfo;
-(NSString*) myDomainfromUrl;





// 子类必须调用 基类
- (void)webViewDidStartLoad:(UIWebView *)webView;
- (void)webViewDidFinishLoad:(UIWebView *)webView;
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
@end

#endif
